<?php


namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Article extends Base
{

    public function article_manage()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;

            $res = Db::name('article')
                ->join("iw_article_class","iw_article_class.id=iw_article.class_id")
                ->field("iw_article_class.id,iw_article.id as aid,iw_article_class.name,iw_article.class_id,iw_article.title,iw_article.click_rate,iw_article.create_time,
                iw_article.small_img,iw_article.content,iw_article.recommend,iw_article.is_show,iw_article.is_del")
                ->where("iw_article.is_del",0)
                ->page($page, $limit)
                ->order('aid', 'desc')
                ->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['create_time'] = date('Y-m-d H:i', $re['create_time']);
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

            }
            $this->success('ok', '', $res);

        } else {
            $data_clss =  Db::name('article_class')->field("id,name")->select();
            $this->assign('class_data',$data_clss);
            //拿到信息总数
            $count = Db::name('article') ->where("is_del",0)->order('id', 'desc')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }

    //文章搜索
    public function article_search()
    {

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;

        $key = trim(input('keyword'));//去除两边空格
        if ($key) {
            $map["iw_article.title"] = $key;
        }
        $key1 = trim(input('fenlei'));
        if ($key1!="全部") {
            $map["iw_article.class_id"] = $key1;
        }

        $res = Db::name('article')
            ->join("iw_article_class","iw_article_class.id=iw_article.class_id")
            ->field("iw_article_class.id,iw_article.id as aid,iw_article_class.name,iw_article.class_id,iw_article.title,iw_article.click_rate,iw_article.create_time,
                iw_article.small_img,iw_article.content,iw_article.recommend,iw_article.is_show,iw_article.is_del")
            ->page($page, $limit)
            ->where($map)
            ->where("iw_article.is_del",0)
            ->order('aid', 'desc')
            ->select();

        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
        foreach ($res as $index => $re) {
            $res[$index]['create_time'] = date('Y-m-d H:i', $re['create_time']);
            $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

        }

        return json(['code' => 1, 'msg' => 'ok', 'total' => count($res), 'data' => $res]);

    }

    //添加新文章
    public function add_article()
    {
        if (request()->isAjax()) {
            $post = input('');
            $data = $post['data'];
            $data['content'] = $post['content'];
            $data['create_time'] = time();
            // $data['status']=$post['status'];
            unset($data['file']);
            // dump($data);
            $data_id = Db::name('article')->insertGetId($data);
            if ($data_id) {
                return json(['code' => 1, 'msg' => '添加成功']);
            }
            else{
                return json(['code' => 1, 'msg' => '添加失败']);
            }

        } else {
            //拿到所有分类
            $data_clss =  Db::name('article_class')->field("id,name")->select();
            $this->assign('class_data',$data_clss);

            $this->assign('date_str', date('Y-m-d', time()));
            return $this->fetch();
        }
    }

    //编辑文章
    public function edit_article()
    {
        if (request()->isAjax()) {
            $data = input('');
            unset($data['file']);

            $data['create_time'] = strtotime($data['create_time']);
            $res = Db::name('article')->where('id', $data['id'])->update($data);

            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('article')->find($id);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            $res['small_img2'] = $qiniu_config['domain'] . '/' . $res['small_img'] . $q_thumb;

            $res['create_time'] = date('Y-m-d', $res['create_time']);

            $data_clss =  Db::name('article_class')->field("id,name")->select();
            $this->assign('class_data',$data_clss);

            $this->assign('item', $res);
            return $this->fetch();
        }
    }

    //设置是否推荐
    public function set_tuijian()
    {
        $data_id = input('data_id');
        if(Db::name('article')->where('id', $data_id)->update(['recommend' => input('recommend')])){
            return json(['code' => 1, 'msg' => '操作成功']);
        }else{
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    //设置是否展示
    public function set_show()
    {
        $data_id = input('data_id');
        if(Db::name('article')->where('id', $data_id)->update(['is_show' => input('is_show')])){
            return json(['code' => 1, 'msg' => '操作成功']);
        }else{
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    //文章删除
    public function del()
    {
        $data = input('');
        $ids = $data['ids'];
        if (!$ids) {
            $this->error('异常');
        }else{
            if(Db::name('article')->where('id', 'in', $ids)->update(['is_del' => 1])){
                return json(['code' => 1, 'msg' => '操作成功']);
            }else{
                return json(['code' => 0, 'msg' => '操作失败']);
            }
        }
    }


    public function li()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;

            $res = Db::name('article_manage')->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['release_time'] = date('Y-m-d H:i:s', $re['release_time']);
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('article_manage')->order('id', 'desc')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }


    }


    public function article_fenlei()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;


            $res = Db::name('article_fenlei')->page($page, $limit)->select();

            $this->success('ok', '', $res);

        } else {
            $count = Db::name('article_fenlei')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }


    }



    //编辑分类
    public function edit_fenlei()
    {
        echo 'ssssss';
    }

    public function edit_article_fenlei(Request $request)
    {
        if (request()->isAjax()) {

            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $fname = input('fname');

            $res = Db::name('article_manage')->page($page, $limit)->where('fenlei', $fname)->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['release_time'] = date('Y-m-d H:i:s', $re['release_time']);
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

            }
            $this->success('ok', '', $res);

        } else {
            $fname = input('fname');
            $count = Db::name('article_manage')->where('fenlei', $fname)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
//
//        $type = $request->param("fenlei");
//        dump($type);
//        $data = Db::name('article_manage')->where('fenlei',$type)->select();
//       $this->assign("item",$data);
    }


}
